import { bindable } from 'aurelia-framework';
import chatService from 'chat/chat-service';

export class Task {

    rightSidebarShow = false;
    isHide = true;

    /**
     * 构造函数
     */
    constructor() {
        // this.subscribe = ea.subscribe(nsCons.EVENT_BLOG_VIEW_CHANGED, (payload) => {
        //     this.routeConfig && this.routeConfig.navModel.setTitle(`${payload.title} | 博文 | TMS`);
        // });
        // this.subscribe1 = ea.subscribe(nsCons.EVENT_BLOG_RIGHT_SIDEBAR_TOGGLE, (payload) => {
        //     if (payload.justRefresh) {
        //         return;
        //     }
        //     if (payload && !_.isUndefined(payload.isHide)) {
        //         this.rightSidebarShow = !payload.isHide;
        //     } else {
        //         this.rightSidebarShow = !this.rightSidebarShow;
        //     }
        // });
        // this.subscribe2 = ea.subscribe(nsCons.EVENT_BLOG_TOGGLE_SIDEBAR, (payload) => {
        //     this.isHide = payload;
        // });
    }

    /**
     * 当数据绑定引擎从视图解除绑定时被调用
     */
    unbind() {
        // this.subscribe.dispose();
        // this.subscribe1.dispose();
        // this.subscribe2.dispose();

        clearInterval(this.timeagoTimer);
    }

    /**
     * 当视图被附加到DOM中时被调用
     */
    attached() {

        let tg = timeago();
        this.timeagoTimer = setInterval(() => {
            $(this.blogContainerRef).find('[data-timeago]').each((index, el) => {
                $(el).text(tg.format($(el).attr('data-timeago'), 'zh_CN'));
            });
        }, 5000);

        // 用户信息popup
        $('.tms-task').on('mouseenter', 'span[data-value].at-user:not(.pp-not),a[data-value].author:not(.pp-not)', (event) => {
            event.preventDefault();
            let target = event.currentTarget;

            if (this.hoverTimeoutRef) {
                if (this.hoverUserTarget === target) {
                    return;
                } else {
                    clearTimeout(this.hoverTimeoutRef);
                    this.hoverTimeoutRef = null;
                }
            }
            this.hoverUserTarget = target;

            this.hoverTimeoutRef = setTimeout(() => {
                ea.publish(nsCons.EVENT_CHAT_MEMBER_POPUP_SHOW, {
                    username: $(target).attr('data-value'),
                    target: target
                });
                this.hoverTimeoutRef = null;
            }, 500);
        });

        // 用户信息popup
        $('.tms-task').on('mouseleave', 'span[data-value].at-user:not(.pp-not),a[data-value].author:not(.pp-not)', (event) => {
            event.preventDefault();
            if (this.hoverTimeoutRef) {
                if (this.hoverUserTarget === event.currentTarget) {
                    clearTimeout(this.hoverTimeoutRef);
                    this.hoverTimeoutRef = null;
                }
            }
        });

        // $('.tms-blog .em-blog-content').on('click', 'a.avatar[data-value], a.author[data-value], .at-user[data-value]', (event) => {
        //     event.preventDefault();
        //     ea.publish(nsCons.EVENT_BLOG_COMMENT_MSG_INSERT, {
        //         content: `{~${$(event.currentTarget).attr('data-value')}} `
        //     });
        // });

    }

    activate(params, routeConfig, navigationInstruction) {

        this.routeConfig = routeConfig;
        nsCtx.taskId = params.id;

        ea.publish(nsCons.EVENT_TASK_SWITCH, { id: params.id });

        return Promise.all([chatService.loginUser().then((user) => {
                nsCtx.loginUser = user;
                nsCtx.isSuper = utils.isSuperUser(user);
                nsCtx.isAdmin = utils.isAdminUser(user);
            }),
            chatService.listUsers(true).then((users) => {
                nsCtx.users = users;
                window.tmsUsers = users;
            })
        ]);
    }
}
